package com.example.config.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@WebFilter(urlPatterns = "/*")
public class ConstantsFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String path = httpRequest.getContextPath();
        int port = httpRequest.getServerPort();
        String basePath = httpRequest.getScheme() + "://" + request.getServerName()
                + (port == 80 ? "" : (":" + port)) + path;
        request.setAttribute("rootPath",basePath);
        request.setAttribute("basePath",basePath);
        request.setAttribute("jsResourceVersion","?version=0.1.21");

        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
